package com.snail.billing.fragment.view;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.text.Html;
import android.text.Spanned;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.android.util.ResUtil;
import com.snail.billing.Resource;
import com.snail.billing.json.JsonCards;

public class CardsAdapter extends SimpleAdapter {

	private static final String IMAGE = "image";
	private static final String TEXT = "text";

	private static final int resource = ResUtil.getLayoutId(Resource.layout.snailbilling_pay_one_item);
	private static final String[] from = { IMAGE };
	private static final int[] to = { ResUtil.getViewId(Resource.id.snailbilling_pay_one_image) };

	private static List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();

	private OnItemClickButtonListener clickButtonListener;

	public CardsAdapter(Context context, JsonCards jsonCards) {
		super(context, data, resource, from, to);
		data.clear();
		Map<String, Object> map;
		for (JsonCards.Card card : jsonCards.getCards()) {
			map = new HashMap<String, Object>();
			map.put(IMAGE, ResUtil.getDrawableId(Resource.drawable.snailbilling_icon_gold));
			Spanned spanned = Html.fromHtml("<span>" + card.getMoney() + card.getCurrencyName() + "\t￥<font color=\"#ff363f\">" + card.getPrice() + "</font>元</span>");
			map.put(TEXT, spanned);
			data.add(map);
		}
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		convertView = super.getView(position, convertView, parent);
		TextView textView = (TextView) convertView.findViewById(ResUtil.getViewId(Resource.id.snailbilling_pay_one_text));
		Object value = data.get(position).get(TEXT);
		if (value != null && value instanceof Spanned) {
			Spanned spanned = (Spanned) value;
			textView.setText(spanned);
		}
		View view = convertView.findViewById(ResUtil.getViewId(Resource.id.snailbilling_pay_one_button));
		view.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (clickButtonListener != null) {
					clickButtonListener.onItemClickButtonListener(position, v);
				}
			}

		});
		return convertView;
	}

	@Override
	public void setViewImage(ImageView imageView, int value) {
		imageView.setImageResource(value);
	}

	public void setOnItemClickButtonListener(OnItemClickButtonListener clickButtonListener) {
		this.clickButtonListener = clickButtonListener;
	}
}